window.onload = function (){
    showData();
}

function showData(){


    axios.get("/user/findAll").then(resp => {
        var info = resp.data;


        if (info === "noland"){
            location.href="/login.html?errorInfo=noland";
        }

        var str="";
        info.forEach(n =>{
            str+=`<tr><td class="alt">${n.nickName}</td>
                      <td class="alt">${n.pwd}</td>
                      <td class="alt">${n.name}</td>
                      <td class="alt">${n.level}</td>
                    <td><input class="button" type="button" value="删除用户" onclick="delUser(${n.id})">
                    <input class="button" type="button" value="用户等级修改" onclick="showChangeLevel(${n.id})">
                    </td></tr>`;
        })
        $("data").innerHTML=str;
    })
}


function showAddUser(){
    $("addUser").style.visibility="visible";
}


function backToUserMain(){
    $("addUser").style.visibility="hidden";
    $("changeUserLevel").style.visibility="hidden"
}

function addNewUser(){
    axios.get("/user/add",{
        params:{
            name:$("newName").value,
            nickName:$("newUserName").value,
            pwd:123,
            level:$("level").value
        }
    }).then(resp =>{
        var info = resp.data;
        if (info == "ok"){
            backToUserMain();
            showData();
        }
    })
}


function delUser(id){
    axios.get("/user/del",{
        params: {
            id
        }
    }).then(resp =>{
        var info = resp.data;
        if (info == "ok"){
            showData();
        }
    })
}


function showChangeLevel(id){
    $("changeUserLevel").style.visibility="visible"
    axios.get("/user/findById",{
        params:{
            id
        }
    }).then(resp =>{
        var info = resp.data;
        $("changeUserId").value=info.id;

        $("changeUserNickName").innerHTML=info.nickName;
        $("changeUserName").innerHTML=info.name;
    })
    
}

function changeUserLevel(){

    axios.get("/user/updateUserLevel",{
        params:{
            id:$("changeUserId").value,
            level: $("changeLevel").value
        }
    }).then(resp =>{
        var info = resp.data;
        if (info == "ok"){
            backToUserMain();
            showData();
        }
    })
}